"""
研究核心接口定义
定义研究助理核心模块的抽象接口和契约
"""

from abc import ABC, abstractmethod
from typing import Dict, Any, Optional, List
from dataclasses import dataclass
from datetime import datetime


@dataclass
class ResearchResult:
    """研究结果数据类"""
    question: str
    answer: str
    search_strategy: str
    research_complete: bool
    processing_time: float
    session_id: Optional[str] = None
    timestamp: str = ""
    success: bool = True
    error: Optional[str] = None
    metadata: Optional[Dict[str, Any]] = None
    
    def __post_init__(self):
        if self.timestamp == "":
            self.timestamp = datetime.now().isoformat()
        if self.metadata is None:
            self.metadata = {}


class IWorkflowEngine(ABC):
    """工作流引擎接口"""
    
    @abstractmethod
    def initialize(self) -> bool:
        """初始化工作流引擎"""
        pass
    
    @abstractmethod
    def execute(self, initial_state: Dict[str, Any], config: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        """执行工作流"""
        pass
    
    @abstractmethod
    def get_status(self) -> Dict[str, Any]:
        """获取工作流状态"""
        pass


class IStateManager(ABC):
    """状态管理接口"""
    
    @abstractmethod
    def create_state(self, initial_data: Dict[str, Any]) -> str:
        """创建新状态并返回状态ID"""
        pass
    
    @abstractmethod
    def get_state(self, state_id: str) -> Dict[str, Any]:
        """获取指定状态"""
        pass
    
    @abstractmethod
    def update_state(self, state_id: str, updates: Dict[str, Any]) -> bool:
        """更新状态"""
        pass
    
    @abstractmethod
    def delete_state(self, state_id: str) -> bool:
        """删除状态"""
        pass


class INode(ABC):
    """节点接口"""
    
    @abstractmethod
    def execute(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """执行节点逻辑"""
        pass
    
    @abstractmethod
    def get_name(self) -> str:
        """获取节点名称"""
        pass
    
    @abstractmethod
    def get_description(self) -> str:
        """获取节点描述"""
        pass


class IResearchCore(ABC):
    """研究核心接口"""
    
    @abstractmethod
    def process_research(self, question: str, session_id: Optional[str] = None) -> ResearchResult:
        """处理单个研究请求"""
        pass
    
    @abstractmethod
    def batch_process(self, questions: List[str], session_id: Optional[str] = None) -> Dict[str, Any]:
        """批量处理研究请求"""
        pass
    
    @abstractmethod
    def get_capabilities(self) -> Dict[str, Any]:
        """获取研究核心能力信息"""
        pass
    
    @abstractmethod
    def health_check(self) -> Dict[str, Any]:
        """健康检查"""
        pass


class IPlugin(ABC):
    """插件接口"""
    
    @abstractmethod
    def initialize(self, config: Dict[str, Any]) -> bool:
        """初始化插件"""
        pass
    
    @abstractmethod
    def get_name(self) -> str:
        """获取插件名称"""
        pass
    
    @abstractmethod
    def get_version(self) -> str:
        """获取插件版本"""
        pass
    
    @abstractmethod
    def get_capabilities(self) -> List[str]:
        """获取插件能力列表"""
        pass
    
    @abstractmethod
    def execute(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
        """执行插件功能"""
        pass


# 导出接口
__all__ = [
    "ResearchResult",
    "IWorkflowEngine",
    "IStateManager",
    "INode",
    "IResearchCore",
    "IPlugin"
]
